package cn.snmp.mutual.org.opennms.protocols.snmp;

import java.util.*;
import java.io.*;

class SnmpUtil {
    static ArrayList m_syntaxArray;

    static boolean registerSyntax(final SnmpSyntax obj) {
        boolean rc = false;
        synchronized (SnmpUtil.m_syntaxArray) {
            boolean addIt = true;
            for (int x = 0; x < SnmpUtil.m_syntaxArray.size(); ++x) {
                final SnmpSyntax tmp = (SnmpSyntax) SnmpUtil.m_syntaxArray.get(x);
                if (obj.typeId() == tmp.typeId()) {
                    addIt = false;
                    break;
                }
            }
            if (addIt) {
                rc = SnmpUtil.m_syntaxArray.add(obj);
            }
        }
        return rc;
    }

    static SnmpSyntax getSyntaxObject(final byte asnType) {
        SnmpSyntax obj = null;
        switch (asnType) {
            case 2: {
                obj = new SnmpInt32();
                break;
            }
            case 65: {
                obj = new SnmpCounter32();
                break;
            }
            case 66: {
                obj = new SnmpGauge32();
                break;
            }
            case 70: {
                obj = new SnmpCounter64();
                break;
            }
            case 67: {
                obj = new SnmpTimeTicks();
                break;
            }
            case 4: {
                obj = new SnmpOctetString();
                break;
            }
            case 68: {
                obj = new SnmpOpaque();
                break;
            }
            case 64: {
                obj = new SnmpIPAddress();
                break;
            }
            case 6: {
                obj = new SnmpObjectId();
                break;
            }
            case 71: {
                obj = new SnmpV2PartyClock();
                break;
            }
            case -127: {
                obj = new SnmpNoSuchInstance();
                break;
            }
            case Byte.MIN_VALUE: {
                obj = new SnmpNoSuchObject();
                break;
            }
            case -126: {
                obj = new SnmpEndOfMibView();
                break;
            }
            case 5: {
                obj = new SnmpNull();
                break;
            }
        }
        if (obj == null) {
            synchronized (SnmpUtil.m_syntaxArray) {
                for (int x = SnmpUtil.m_syntaxArray.size() - 1; x >= 0; --x) {
                    final SnmpSyntax o = (SnmpSyntax) SnmpUtil.m_syntaxArray.get(x);
                    if (asnType == o.typeId()) {
                        obj = o.duplicate();
                        break;
                    }
                }
            }
        }
        return obj;
    }

    static void copy(final byte[] src, final int srcOffset, final byte[] dest, final int destOffset, final int count) {
        for (int x = 0; x < count; ++x) {
            dest[destOffset + x] = src[srcOffset + x];
        }
    }

    static void copy(final int[] src, final int srcOffset, final int[] dest, final int destOffset, final int count) {
        for (int x = 0; x < count; ++x) {
            dest[destOffset + x] = src[srcOffset + x];
        }
    }

    static void rotate(final byte[] buf, final int begin, final int pivot, final int end) {
        final int dist = end - pivot;
        final byte[] hold = new byte[dist];
        copy(buf, pivot, hold, 0, dist);
        for (int x = pivot - begin - 1; x >= 0; --x) {
            buf[begin + dist + x] = buf[begin + x];
        }
        copy(hold, 0, buf, begin, dist);
    }

    static void rotate(final int[] buf, final int begin, final int pivot, final int end) {
        final int dist = end - pivot;
        final int[] hold = new int[dist];
        copy(buf, pivot, hold, 0, dist);
        for (int x = pivot - begin - 1; x >= 0; --x) {
            buf[begin + dist + x] = buf[begin + x];
        }
        copy(hold, 0, buf, begin, dist);
    }

    static void dumpHex(final PrintStream out, final byte[] data, int offset, int length) {
        if (offset + length > data.length) {
            return;
        }
        while (length > 0) {
            final byte b = data[offset];
            out.print("0x");
            out.print(Integer.toHexString(b >> 4 & 0xF));
            out.print(Integer.toHexString(b & 0xF));
            out.print(" ");
            --length;
            ++offset;
        }
    }

    static {
        SnmpUtil.m_syntaxArray = null;
        SnmpUtil.m_syntaxArray = new ArrayList();
    }
}
